package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.*;
import com.itheima.exception.CustExcepiton;
import com.itheima.result.R;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public R getPageList(Integer page, Integer pageSize) {
        //分页构造器对象
        Page<Setmeal> PageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(PageInfo, lqw);
        Page<SetmealDto> pageDto = new Page<>();
        BeanUtils.copyProperties(PageInfo, pageDto, "records");
        List<SetmealDto> collect = PageInfo.getRecords().stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Category byId = categoryService.getById(item.getCategoryId());
            String name = byId.getName();
            setmealDto.setCategoryName(name);
            return setmealDto;
        }).collect(Collectors.toList());
        pageDto.setRecords(collect);
        return R.success(pageDto);
    }

    @PostMapping
    @CacheEvict(value = "setmeal", allEntries = true)
    public R updateList(@RequestBody SetmealDto setmealDto) {
        log.info("setmeal:{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("成功");
    }

    @PostMapping("/status/{status}")
    public R updateStatusList(@PathVariable Integer status, @RequestParam ArrayList<Long> ids) {
        LambdaUpdateWrapper<Setmeal> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Setmeal::getId, ids);
        lambdaUpdateWrapper.set(Setmeal::getStatus, status);
        boolean update = setmealService.update(lambdaUpdateWrapper);
        return R.success("修改成功");
    }

    @DeleteMapping
    @Transactional()
    @CacheEvict(value = "setmeal", allEntries = true)
    public R deleteByIds(@RequestParam ArrayList<Long> ids) {
        log.info("看的见IDS：{}", ids);
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        queryWrapper.eq("status", 1);
        int count = setmealService.count(queryWrapper);
        if (count != 0) {
            throw new CustExcepiton("套菜正在出售，不能删除");
        }

        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(lambdaQueryWrapper);

        setmealService.removeByIds(ids);
        return R.success("删除成功");
    }

    @GetMapping("/dish/{id}")
    public R getSetmealDish(@PathVariable Long id) {
        log.info("这个id:{}", id);
        Dish byId = dishService.getById(id);
        return R.success(byId);
    }

    @GetMapping("/list")
    @Cacheable(value = "setmeal", key = "#setmeal.categoryId + '_' + #setmeal.status")
    public R getAppList(Setmeal setmeal) {
        log.info("setmeal:{}", setmeal);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        qw.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        //        查询套餐
        List<Setmeal> list = setmealService.list(qw);

        return R.success(list);
    }
}
